package com.siwei.mes.controller.materials;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.system.SupplierCompanyMaterials;
import com.siwei.mes.service.system.SupplierCompanyMaterialsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 供应商物料表相关
 *
 * @author linzi
 * @since 2024-07-17 23:04:40
 */
@Api(value = "供应商物料表")
@RestController
@RequestMapping("supplierCompanyMaterials")
public class SupplierCompanyMaterialsController {
    @Resource
    private SupplierCompanyMaterialsService supplierCompanyMaterialsService;

    @ApiOperation(value = "新增供应商物料表  这里作废了")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) SupplierCompanyMaterials entity) {
        return Json.succ().data(ApiResult.addColumn("id", supplierCompanyMaterialsService.create(entity)));
    }

    @ApiOperation(value = "修改供应商物料表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) SupplierCompanyMaterials entity) {
        supplierCompanyMaterialsService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除供应商物料表  这里作废了")
    @PostMapping("/delete")
    public Json delete(@RequestBody SupplierCompanyMaterials entity) {
        supplierCompanyMaterialsService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取供应商物料表  这个主表是erp的供应商物料  companyyclrelation表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<SupplierCompanyMaterials, SupplierCompanyMaterials> query) {
        return Json.succ().data(supplierCompanyMaterialsService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取供应商物料表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(supplierCompanyMaterialsService.loadById(id));
    }

    @ApiOperation(value = "获取所有供应商物料表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody SupplierCompanyMaterials entity) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getAll(entity)));
    }

    @ApiOperation(value = "供应商物料统计")
    @PostMapping("getStatistics")
    public Json getStatistics(@RequestBody SupplierCompanyMaterials entity) {
        assertTrue(entity.getSupplierCompanyId() == null, "供应商id为空");
        return Json.succ().data(supplierCompanyMaterialsService.getStatistics(entity.getSupplierCompanyId()));
    }

    @ApiOperation("根据材料名称+材料规格+供应商id获取所有生产厂家")
    @GetMapping("getFactoryList")
    public Json getFactoryList(@RequestParam("supplierCompanyId") Long supplierCompanyId, @RequestParam("materialsName") String materialsName, @RequestParam("materialsSpec") String materialsSpec) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getFactoryList(supplierCompanyId, materialsName, materialsSpec)));
    }

    /**
     * 根据物料类型获取了所有生产厂家
     *
     * @param materialsType 1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
     * @return
     */
    @ApiOperation("根据物料类型获取了所有生产厂家")
    @GetMapping("getFactoryListByMaterialsType")
    public Json getFactoryListByMaterialsType(@RequestParam("materialsType") Integer materialsType) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getFactoryListByMaterialsType(materialsType)));
    }


    /**
     * 根据物料类型获取所有物料名称
     *
     * @param materialsType 1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
     * @return
     */
    @ApiOperation(value = "根据物料类型获取所有物料名称")
    @GetMapping("getMaterialsNameList")
    public Json getMaterialsNameList(@RequestParam("materialsType") Integer materialsType) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getMaterialsNameList(materialsType)));
    }

    /**
     * 根据物料类型+物料名称获取所有物料规格
     *
     * @param materialsType 1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
     * @param materialsName 物料名称
     * @return
     */
    @ApiOperation(value = "根据物料类型+物料名称获取所有物料规格")
    @GetMapping("getMaterialsSpecList")
    public Json getMaterialsSpecList(@RequestParam("materialsType") Integer materialsType, @RequestParam("materialsName") String materialsName) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getMaterialsSpecList(materialsType, materialsName)));
    }

    /**
     * 根据物料类型+物料名称+物料规格获取所有生产厂家
     *
     * @param materialsType 1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
     * @param materialsName 物料名称
     * @param materialsSpec 物料规格
     * @return
     */
    @ApiOperation(value = "根据物料类型+物料名称+物料规格获取所有生产厂家")
    @GetMapping("getMaterialsFactoryList")
    public Json getMaterialsFactoryList(@RequestParam("materialsType") Integer materialsType,
                                        @RequestParam("materialsName") String materialsName,
                                        @RequestParam(required = false, name = "materialsSpec") String materialsSpec) {
        return Json.succ().data(ApiResult.addList(supplierCompanyMaterialsService.getMaterialsFactoryList(materialsType, materialsName, materialsSpec)));
    }
}
